<?php
require_once("db/DAO/CartDAO.php");

class CartController {
	
	private $cartDAO;
	
	public function __construct(){
		$this->cartDAO = new CartDAO();
	}
	
	public function saveToCart($idCart, $itemId){
		
		$exist = $this->cartDAO->loadItem($idCart,$itemId);
		
		if(!is_null($exist)){
			$this->cartDAO->increaseCountOfItem($idCart,$itemId);			
		} else {
			$this->cartDAO->insertItemById($idCart,$itemId);
		}
	} 

	public function loadAllItemsInCart($cartId){
		return $this->cartDAO->loadAllItems($cartId);
	}	
	
	public function removalCheck($idCart){
		
		$remove = Utils::requestPost('del');

		if($remove==1){
			$this->cartDAO->removeAllItems($idCart);
		}elseif (!is_null($remove) && $remove>1) {
			$this->cartDAO->removeItemById($idCart,$remove);
		} 
	}
	
	public function getItemCount($idCart){
		return $this->cartDAO->getCountOfItemsInCart($idCart);
	}
	
	public function renderCartItems($obsah){
		
	echo "<table cellspacing='2' cellpadding='2'>";
	
	echo "<tr>" .
			"<th>ID</th>" .
			"<th>Nazev</th>" .
			"<th>Pocet</th>" .
			"<th>Cena/Kus</th>" .
			"<th>Cena/Celkem</th>" .
		"</tr>";
	
	foreach ( $obsah as $value ) {
    	
    	$opis = $value->getProductId();
    	
    	echo "<tr align='center'>";
				echo "<td style='padding-left:15px;'>";
					echo $value->getProductId();
				echo "</td>";
				
				echo "<td style='padding-left:15px;'>";
					echo $value->getProductName();
				echo "</td>";
				
				echo "<td style='padding-left:15px;'>";
					echo $value->getProductCount();
				echo "</td>";
				
				echo "<td style='padding-left:15px;'>";
					echo $value->getProductPrice();
				echo "</td>";
				
				echo "<td style='padding-left:15px;'>";
					echo $value->getProductPrice()*$value->getProductCount();
				echo "</td>";
				
				echo "<td style='padding-left:15px;'>";
					echo "<form action='cart.php' method='post'>";
					echo "<input type='hidden' name='del' value='$opis'>";
					echo "<input type='submit' value='Odebrat z kosiku' style='border:1px solid black;background-color: transparent;font-size: 10px;font-family: verdana;'>";
					echo "</form>";
				echo "</td>";
				
				
		echo "</tr>";
  	}
	echo "</table>";
	 
		echo "<form action='cart.php' method='post'>";
		echo "<input type='hidden' name='del' value='1'>";
		//echo "<input type='hidden' name='ref' value='".$ref."'>";
		echo "<input type='submit' value='Vyprazdnit kosik'>";
		echo "</form>";
 	}
	
}

?>
